$(function() {

	layui.use(['form', 'layer', 'laydate', 'util'], function() {
		var form = layui.form;
		form.render();
		var layer = layui.layer;
		var laydate = layui.laydate;
		var util = layui.util;

		laydate.render({
			elem: "#birthday",
			type: "date"
		});

		function find_currentUser_info() {
			$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/sysUser/findMyUserVo",
				async: true,
				"success": function(result) {
					if(result.resultCode == 0) {
						build_currentUser_info(result);
					}

				}

			});
		}

		function build_currentUser_info(result) {
			var data = result.data;

			$("input[name='user.account']").val(data.user.account);
			$("input[name='user.name']").val(data.user.name);
			$("input[name='user.phone']").val(data.user.phone);
			$("input[name='user.birthday']").val(util.toDateString(data.user.birthday, "yyyy-MM-dd"));
			var sex = ["input[title='保密']", "input[title='男']", "input[title='女']"];
			$(sex[data.user.sex]).prop("checked", true);
			$("input[name='user.email']").val(data.user.email);
			var version = data.user.version;
			$("input[name='user.version']").val(version);
			form.render();
		}

		form.on("submit(updateUser)", function(data) {
			var new_pass=$("input[name='user.password']").val();
			var confirm_pass=$("input[name='confirmPassword']").val();
			if(new_pass==confirm_pass){
					var params = data.field;
			$.ajax({
				type: "post",
				url: "http://localhost:8080/ssmService/sysUser/updateMyUserVo",
				data: params,
				"success": function(result) {
					if(result.resultCode == 0) {
						layer.msg(result.message, {
							icon: 1,
							time: 1000
						});
						location.reload();
					} else {
						layer.msg(result.message, {
							icon: 2,
							time: 1000
						});
					}

				},
				"error": function() {
					layer.msg("修改失败，请重试", {
						icon: 2,
						time: 1000
					});
				}
			});
				
			}else{
					layer.msg("修改失败，两次输入的密码不一致", {
						icon: 2,
						time: 1000
					});
			}
			
		

			return false;
		});
		var index = 0;
		$(".updatePassword").click(function() {
			index++;
			console.log(index);
			if(index % 2 != 0) {
				var passName = ["旧密码:", "新密码:", "确认密码:"];
				var pass_name = ["old_password", "user.password", "confirmPassword"];
				for(var i = 0; i < 3; i++) {
					var bigDiv = $("<div></div>").addClass("layui-form-item");
					var label = $("<label></label>").addClass("layui-form-label").text(passName[i]);
					var minDiv = $("<div></div>").addClass("layui-input-block");
					var passInput = $("<input type='text' name='" + pass_name[i] + "' required  lay-verify='required' placeholder='请输入密码' autocomplete='off' class='layui-input'>")
					minDiv.append(passInput);
					bigDiv.append(label).append(minDiv);
					$(".password").append(bigDiv);
				}
				$(this).text("取消修改密码");
			} else {
				$(".password").empty();
				$(this).text("修改密码");
			}

			form.render();
			return false;
		});

		find_currentUser_info();

	});
})